<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="param">
                <el-form-item>
                    <el-input v-model="param.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="param.state" placeholder="请选择状态" clearable>
                        <el-option label="上架" value="1"></el-option>
                        <el-option label="下架" value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="searchKeyword">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="onsele" :disabled="this.sels.length===0">上架</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="warning" @click="offsale" :disabled="this.sels.length===0">下架</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="empList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="宠物名" width="140" >
            </el-table-column>
            <el-table-column prop="saleprice" label="出售价" width="100" >
            </el-table-column>
            <el-table-column prop="offsaletime" label="下架时间" width="180" >
            </el-table-column>
            <el-table-column prop="onsaletime" label="上架时间" min-width="180" >
            </el-table-column>
            <el-table-column prop="state" label="状态" width="150" :formatter="formatState">
            </el-table-column>
            <el-table-column prop="costprice" label="成本" min-width="200" >
            </el-table-column>
            <el-table-column label="操作" width="250">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="param.pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>



        <!--新增\编辑界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="宠物名" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="成本">
                    <el-input v-model="addForm.costprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="卖价">
                    <el-input v-model="addForm.saleprice" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="图片">
                    <el-upload
                            class="upload-demo"
                            action="http://localhost:8080/fastdfs/upload"
                            :on-success="handleSuccess"
                            :file-list="fileList"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
                    </el-upload>
                </el-form-item>

                <el-form-item label="宠物介绍" prop="intro">
                    <div class="edit_container">
                        <quill-editor
                                v-model="addForm.detail.intro"
                                :options="quillConfig"
                                >
                        </quill-editor>
                    </div>
                </el-form-item>

                <el-form-item label="领养须知" prop="intro">
                    <div class="edit_container">
                        <quill-editor
                                v-model="addForm.detail.adoptNotice"
                                :options="quillConfig"
                                >
                        </quill-editor>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>

    import  {quillEditor}  from "vue-quill-editor"; //调用编辑器
    import 'quill/dist/quill.core.css';
    import 'quill/dist/quill.snow.css';
    import 'quill/dist/quill.bubble.css';
    import quillConfig from "../../common/js/quill-config.js" //引入配置
    export default {
        components: {
            quillEditor
        },
        name: "emp",
        data(){
            return {
                quillConfig: quillConfig,
                total:0,
                param:{
                    currentPage:1,
                    pageSize:5,
                    keyword:''
                },
                // 列表加载
                listLoading:false,
                // 列表展示数据
                empList:{},
                sels:[],
                // 模态框显示
                addFormVisible:false,
                //模态框数据
                addForm:{
                    name:'',
                    saleprice:'',
                    costprice:'',
                    resources:'',
                    state:0,
                    detail:{
                        intro:"",
                        adoptNotice:''
                    }
                },
                //图片上传
                fileList:[],
                // 模态框规则
                addFormRules:{
                    name: [
                        { required: true, message: '请输入宠物名', trigger: 'blur' }
                    ],
                },
                // 提交按钮加载动画
                addLoading:false,
            }
        },
        methods:{
            // 高级查询
            searchKeyword(){
                this.param.currentPage=1;
                this.loadList();
            },
            // 新增按钮弹框
            handleAdd(){
                this.addForm={
                    name:'',
                    saleprice:'',
                    costprice:'',
                    resources:'',
                    state:0,
                    detail:{
                        intro:"",
                        adoptNotice:''
                    }
                };
                this.fileList=[];
                this.addFormVisible = true;
            },
            //文件上传成功回调
            handleSuccess(response, file, fileList){
                // console.log(fileList);
                // this.addForm.logo = response.resultObj;
                this.addForm.resources=[];
                for(var i = 0;i<fileList.length;i++){
                    if(i==fileList.length-1){
                        this.addForm.resources+=fileList[i].response.resultObj;
                    }else{
                        this.addForm.resources+=fileList[i].response.resultObj+',';
                    }
                }
            },
            //每项前面的勾选
            selsChange(val){
                this.sels = val;
            },
            formatState(row){
                return row.state == 1 ? '上架' : '下架';
            },
            // 编辑按钮
            handleEdit(index,row){
                this.addFormVisible=true;
                this.addForm=Object.assign({},row);
                // console.log(row);
                this.fileList=[];
                //图片数据的回显
                //this.fileList=[{name:"xx",url:"xx"},{}];
                let picArr =row.resources.split(",");
                for (let i =0;i<picArr.length;i++){
                    let obj ={
                        name:"图片"+(i+1),
                        url:"http://47.95.117.210:8888"+picArr[i]
                    }
                    this.fileList.push(obj);
                }

            },
            // 删除按钮
            handleDel(index,row){
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.delete("/pet/" + row.id).then(res => {
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            // 批量上架
            onsele(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认上架吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/pet/onsale",ids).then(res=>{
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            offsale(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认下架吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/pet/offsale",ids).then(res=>{
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })
            },
            // 批量删除
            batchRemove(){
                // 拿到勾选的id数组
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.$http.patch("/pet",ids).then(res=>{
                        this.loadList();
                        if (res.data.success) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: '网络异常',
                                type: 'warning'
                            });
                        }
                    })
                }).catch(()=>{
                    this.$message('已取消删除');
                })

            },
            // 当页码改变的时候
            handleCurrentChange(val){
                this.param.currentPage = val;
                this.loadList();
            },
            //模态框提交
            addSubmit(){
                this.$http.put("/pet",this.addForm).then(res=>{
                    this.addFormVisible=false;
                    this.loadList();
                    if(res.data.success){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                    }else{
                        this.$message({
                            message: '网络异常',
                            type: 'warning'
                        });
                    }
                })
            },
            // 加载数据
            loadList(){
                this.$http.post("/pet",this.param).then(res=>{
                    this.empList = res.data.data;
                    this.total=res.data.total;
                })
            }
        },
        mounted(){
            this.loadList();
        }
    }
</script>

<style scoped>

</style>